<?php

namespace Application\controller;

class ReportController extends BaseLoginController
{
    /**
     * 上报监控玩家物品
     * @return void
     */
    public function monitorItem()
    {

        $env = APP_DEV === 0 ? '正式环境' : '内网环境';
        $openid = $this->get('openId');
        $sid = $this->get('sid');
        $actorId = $this->get('actorId');
        $actorName = $this->get('actorName');
        $level = $this->get('level');
        $vip = $this->get('vip');
        $amount = $this->get('amount');
        $realAmount = $this->get('realAmount');
        $item = $this->get('items'); // items:1_10000000_73831617,1_10000000_65159640
        $reason = $this->get('reason');
        $actionId = $this->get('actionId');
        // 先打一个log
        logfile('ReportModel', "[monitorItem] openid:$openid sid:$sid actorId:$actorId actorName:$actorName level:$level vip:$vip amount:$amount realAmount:$realAmount items:$item reason:$reason actionId:$actionId");
        if (empty($item)) $this->error('items is empty');
        $items = explode(',', $item);
        $dt = date("Y-m-d H:i:s");
        $ReportModel = $this->model("Report");

        foreach ($items as $item) {
            if ($item) {
                $data = [];
                list($itemId, $confNum, $actorNum, $time, $type) = explode('_', $item);
                $data['sid'] = $sid;
                $data['account'] = $openid;
                $data['actor_id'] = $actorId;
                $data['actor_name'] = $actorName;
                $data['level'] = $level;
                $data['vip'] = $vip;
                $data['amount'] = $amount;
                $data['gm_amount'] = $realAmount;
                $data['item_id'] = $itemId;
                $data['num'] = $confNum;
                $data['actor_num'] = $actorNum;
                $data['create_dt'] = $dt;
                $data['reason'] = $reason;
                $data['action_id'] = $actionId;
                $data['time'] = $time;
                $data['type'] = 1; // 游戏监控
                $commond = $ReportModel->checkNotify('monitor_player');
                // 0：正式服 1：全部服
                if ($commond['ext1'] === '0') {
                    if ($sid <= 1000) $this->success([]);
                }
                // 同一个玩家 同一个itemId 同一个time，直接更新
                $where = "actor_id='{$actorId}' and item_id='{$itemId}' and time='{$time}'";
                $info = $ReportModel->getMonitorItemInfo($where);

                if ($info && $type==0) {
                    $row['level'] = $level;
                    $row['vip'] = $vip;
                    $row['amount'] = $amount;
                    $row['gm_amount'] = $realAmount;
                    $row['actor_num'] = $actorNum;
                    $res = $ReportModel->updateMonitorItem($row, $where);
                } else {
                    $res = $ReportModel->insertMonitorItem($data);
                    if ($commond['status'] === '1'){
                        $server = $this->model("Server")->getServerBySid($sid);
                        $serverName = $server ? $server['name'] : $sid;
                        $item = $this->model("Config")->getItemById($itemId);
                        $itemName = $item ? $item['name'] : $itemId;
                        $message = $env."【道具监控】提示：\n- 类型：游戏监控\n- 时间：{$dt} \n- 区服：{$serverName}（{$sid}） \n- 玩家：{$actorName}（{$actorId}）\n- 道具：{$itemName}（{$itemId}）\n- 数量：{$actorNum}\n- 警告：已超过监控值{$confNum}，请注意核查！ \n ";
                        feishuNotify(REPORT_NOTIFY_URL, $message);
                    }
                }
            }
        }

        if (!$res) $this->error('insert error');
        $this->success([]);

    }

    public function fightResult()
    {
        $env = APP_DEV === 0 ? '正式环境' : '内网环境';
        $openid = $this->get('openId');
        $sid = $this->get('sid');
        $actorId = $this->get('actorId');
        $actorName = $this->get('actorName');
        $level = $this->get('level');
        $vip = $this->get('vip');
        $amount = $this->get('amount');
        $power = $this->get('power');
        $moduleId = $this->get('moduleId');
        $levelId = $this->get('levelId');
        $targetPower = $this->get('targetPower');
        $targetId = $this->get('targetId');
        $fightType = $this->get('fightType');
        $ext = $this->get('ext');
        // 先打一个log
        logfile('ReportModel', "[fightResult] openid:$openid sid:$sid actorId:$actorId actorName:$actorName level:$level vip:$vip amount:$amount power:$power moduleId:$moduleId levelId:$levelId targetPower:$targetPower targetId:$targetId fightType:$fightType ext:$ext");
        $dt = date("Y-m-d H:i:s");
        $data = [];
        $data['sid'] = $sid;
        $data['account'] = $openid;
        $data['actor_id'] = $actorId;
        $data['actor_name'] = $actorName;
        $data['level'] = $level;
        $data['vip'] = $vip;
        $data['amount'] = $amount;
        $data['power'] = $power;
        $data['module_id'] = $moduleId;
        $data['level_id'] = $levelId;
        $data['target_power'] = $targetPower;
        $data['create_dt'] = $dt;
        $data['target_id'] = $targetId;
        $data['ext'] = $ext;
        $data['type'] = $fightType;
        $ReportModel = $this->model("Report");
        $commond = $ReportModel->checkNotify('fight_result');
        // 0：正式服 1：全部服
        if ($commond['ext1'] === '0') {
            if ($sid <= 1000) $this->success([]);
        }
        $res = $this->model("Report")->insertFightResult($data);
        if ($commond['status'] === '1'){
            $server = $this->model("Server")->getServerBySid($sid);
            $serverName = $server ? $server['name'] : $sid;
            $message =  $env."【战斗监控】提示：\n- 时间：{$dt} \n- 区服：{$serverName}（{$sid}） \n- 玩家：{$actorName}（{$actorId}）\n- 功能：{$moduleId}\n- 关卡：{$levelId}- 对手：{$targetId}- 类型：{$fightType}\n- 警告：战斗异常，请注意核查！\n ";
            feishuNotify(REPORT_NOTIFY_URL, $message);
        }
        if (!$res) $this->error('insert error');
        $this->success([]);
    }


    public function monitorHero()
    {
        $env = APP_DEV === 0 ? '正式环境' : '内网环境';
        $openid = $this->get('openId');
        $sid = $this->get('sid');
        $actorId = $this->get('actorId');
        $actorName = $this->get('actorName');
        $level = $this->get('level');
        $vip = $this->get('vip');
        $amount = $this->get('amount');
        $realAmount = $this->get('realAmount');
        $heros = $this->get('heros'); // items:1_10000000_73831617,1_10000000_65159640
        $reason = $this->get('reason');
        $actionId = $this->get('actionId');


        $params = $this->request->get();
        // 先打一个log
        logfile('ReportModel', '"[monitorHero] '.json_encode(var_export($params,true)));
        if (empty($heros)) $this->error('heros is empty');

        $dt = date("Y-m-d H:i:s");
        /* @var $ReportModel \Application\model\ReportModel */
        $ReportModel = $this->model("Report");
        //foreach ($heros as $item) {
        if ($heros) {
            $data = [];
            list($heroId, $num) = explode('_', $heros);
            $data['sid'] = $sid;
            $data['account'] = $openid;
            $data['actor_id'] = $actorId;
            $data['actor_name'] = $actorName;
            $data['level'] = $level;
            $data['vip'] = $vip;
            $data['amount'] = $amount;
            $data['gm_amount'] = $realAmount;
            $data['hero_id'] = $heroId;
            $data['num'] = $num;
            $data['create_dt'] = $dt;
            $res = $ReportModel->insertMonitorHero($data);
        }
       // }

        if (!($res ?? null)) $this->error('insert error');
        $this->success([]);
    }


}